package com.aat.dxfy.dxyStart;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author wuxin
 * @info 读取xlsm,xlss创建bean dao service controller
 *
 */
public class AutoJavaCode {
	private String baseDir;// 代码生成基本目录
	private String driver;
	private String url;
	private String user;
	private String pwd;
	private String packageName;// 代码包路径

	private String tableName;
	private String moduleName;// 代码所属模块名
	private String beanName;
	private List<String> fileds = new ArrayList<String>();
	private List<String> types = new ArrayList<String>();

	private String javaMakeCodeDir = "javaMakeCodeDir";
	private String packagePath;// 代码包：com.公司名.项目名
	private Connection con;

	public static void main(String[] args) throws Exception {
		String baseDir = "E:\\coreservice";
		String packagePath = "com/sense/cloud";
		String url = "jdbc:mysql://127.0.0.1:3306/coreservice?useUnicode=true&characterEncoding=utf8";
		String user = "root";
		String pwd = "root";
		String driver = "com.mysql.jdbc.Driver";
		AutoJavaCode obj = new AutoJavaCode(baseDir, packagePath, driver, url, user, pwd);
		obj.start();
	}

	public AutoJavaCode(String baseDir, String packagePath, String driver, String url, String user, String pwd) {
		// params
		this.baseDir = baseDir;
		this.driver = driver;
		this.url = url;
		this.user = user;
		this.pwd = pwd;
		this.packagePath = packagePath;
		packageName = packagePath.replaceAll("/", ".");// 包路径

		if (File.separator.equals("\\")) {
			packagePath = packagePath.replaceAll("/", "\\\\");
		}
		//
	}

	// 0,读取xlsm 形式
	public void start() throws IOException, InterruptedException {

		List<String> tabNames = getTabNames();
		for (String s : tabNames) {
			tableName = s;
			String[] ss = tableName.split("_");
			moduleName = ss[0]; // 模块名称
			String str = ss[1]; // 表意义
			beanName = str.substring(0, 1).toUpperCase() + str.substring(1, str.length());

			getTabFileds(tableName);

			String codePath = baseDir + File.separator + javaMakeCodeDir + File.separator + packagePath + File.separator
					+ moduleName + File.separator;
			// 开始
			createJavaBean(codePath);

			createDao(codePath);

			createDaoXml(codePath);

			createService(codePath);

			createServiceImpl(codePath);

			// 结束
		}

		// over
		close();
	}

	// 2,根据读取的信息，创建实体类,{entity}.java
	public void createJavaBean(String codePath) {
		// 创建包
		String javaFilePath = codePath + "bean";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		sb.append("package " + packageName + "." + moduleName + ".bean;\n\n");// 包
		sb.append("import java.io.Serializable;\n");
		sb.append("\n\n");

		sb.append("public class " + beanName + "  implements Serializable{\n");// 类

		sb.append("    private static final long serialVersionUID = 1L;\n");
		for (int i = 0; i < fileds.size(); i++) {
			sb.append("    private " + types.get(i) + " " + fileds.get(i) + ";\n");
		}

		// set get
		sb.append("\n");
		for (int i = 0; i < fileds.size(); i++) {
			String fn1 = fileds.get(i);
			String fn2 = fn1.substring(0, 1).toUpperCase() + fn1.substring(1, fn1.length());
			sb.append("    public " + types.get(i) + " get" + fn2 + "() {\n");
			sb.append("        return " + fn1 + ";\n");
			sb.append("    }\n\n");

			sb.append("    public void set" + fn2 + "(" + types.get(i) + " " + fn1 + ") {\n");
			sb.append("        this." + fn1 + " = " + fn1 + ";\n");
			sb.append("    }\n\n");
		}

		// hashcode
		sb.append("    public int hashCode() {\n");
		sb.append("        final int prime = 31;\n");
		sb.append("        int result = 1;\n");
		sb.append("        result = prime * result + ((id == null) ? 0 : id.hashCode());\n");
		sb.append("        return result;\n");
		sb.append("    }\n\n");

		// equals
		sb.append("    public boolean equals(Object obj) {\n");
		sb.append("        if (this == obj)\n");
		sb.append("            return true;\n");
		sb.append("        if (obj == null)\n");
		sb.append("            return false;\n");
		sb.append("        if (getClass() != obj.getClass())\n");
		sb.append("            return false;\n");
		sb.append("        " + beanName + " other = (" + beanName + ") obj;\n");
		sb.append("        if (id == null) {\n");
		sb.append("            if (other.id != null)\n");
		sb.append("                return false;\n");
		sb.append("        } else if (!id.equals(other.id))\n");
		sb.append("            return false;\n");
		sb.append("        return true;\n");
		sb.append("    }\n\n");

		// tostr
		sb.append("    public String toString() {\n");
		sb.append("       return \"" + beanName + " [id=\"+ id +\"]\";\n");
		sb.append("    }\n\n");

		// ok
		sb.append("}\n");
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + beanName + ".java");
	}

	// 3-1,根据读取的信息，创建实体Dao,{entity}Dao.java\{entity}Dao.xml
	public void createDao(String codePath) {
		// 创建包
		String javaFilePath = codePath + "dao";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		sb.append("package " + packageName + "." + moduleName + ".dao;\n\n");// 包
		sb.append("import java.util.*;\n");
		sb.append("import org.springframework.stereotype.Repository;\n");
		sb.append("import " + packageName + "." + moduleName + ".bean." + beanName + ";\n\n");// 导包

		sb.append("@Repository\n");
		sb.append("public interface " + beanName + "Dao{\n\n");// 类

		sb.append("    int addBean(" + beanName + " bean);\n\n");
		sb.append("    int updateBean(" + beanName + " bean);\n\n");
		sb.append("    int deleteBean(String id);\n\n");
		sb.append("    " + beanName + " queryBean(String id);\n\n");
		sb.append("    List<" + beanName + "> queryAll(Map<String, Object> map);\n\n");
		sb.append("    long queryCount(Map<String, Object> map);\n\n");

		sb.append("}\n");
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + beanName + "Dao.java");
	}

	// 3-2,根据读取的信息，创建实体Dao,{entity}Dao.java\{entity}Dao.xml
	public void createDaoXml(String codePath) {
		// 创建包
		String javaFilePath = codePath + "dao";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		// head
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
		sb.append(
				"<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\n\"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n");
		sb.append("<mapper namespace=\"" + packageName + "." + moduleName + ".dao." + beanName + "Dao\">\n\n");

		// 表
		sb.append("    <!-- 表 -->\n");
		sb.append("    <sql id=\"Table_Name\">\n");
		sb.append("        " + tableName + "\n");
		sb.append("    </sql>\n\n");

		// 字段
		sb.append("    <!-- 字段 -->\n");
		sb.append("    <sql id=\"Base_Column_List\">\n");
		String column = "        ";
		for (int i = 0; i < fileds.size(); i++) {
			if (i != 0 && i % 5 == 0) {
				column += "\n        ";
			}
			String f = fileds.get(i);
			if (isNotEmpty(f)) {
				column += f + ",";
			}
		}
		column = column.substring(0, column.length() - 1);
		sb.append(column + "\n");
		sb.append("    </sql>\n\n");
		//

		// add
		sb.append("    <!-- 新增 -->\n");
		sb.append("    <insert id=\"addBean\" parameterType=\"" + beanName + "\">\n");
		sb.append("        INSERT INTO\n");
		sb.append("        <include refid=\"Table_Name\" />\n");
		sb.append("        (\n");
		sb.append("        <include refid=\"Base_Column_List\" />\n");
		sb.append("        ) VALUES\n");
		sb.append("        (\n");
		String columnkh = "        ";
		for (int i = 0; i < fileds.size(); i++) {
			if (i != 0 && i % 5 == 0) {
				columnkh += "\n        ";
			}
			String f = fileds.get(i);
			if (isNotEmpty(f)) {
				columnkh += "#{" + f + "},";
			}
		}
		columnkh = columnkh.substring(0, columnkh.length() - 1);
		sb.append(columnkh + "\n");
		sb.append("        )\n");
		sb.append("    </insert>\n\n");
		//

		// upt
		sb.append("    <!-- 修改 -->\n");
		sb.append("    <update id=\"updateBean\" parameterType=\"" + beanName + "\">\n");
		sb.append("        UPDATE\n");
		sb.append("        <include refid=\"Table_Name\" />\n");
		sb.append("        <set>\n");
		sb.append("            <include refid=\"updateSQL\"/>\n");
		sb.append("        </set>\n");
		sb.append("        WHERE id = #{id}\n");
		sb.append("    </update>\n\n");

		// del
		sb.append("    <!-- 删除 -->\n");
		sb.append("    <delete id=\"deleteBean\" parameterType=\"java.lang.String\">\n");
		sb.append("        DELETE FROM\n");
		sb.append("        <include refid=\"Table_Name\" />\n");
		sb.append("        WHERE id = #{id}\n");
		sb.append("    </delete>\n\n");

		// queryBean
		sb.append("    <!-- 查询单个 -->\n");
		sb.append("    <select id=\"queryBean\" parameterType=\"java.lang.String\" resultType=\"" + beanName + "\">\n");
		sb.append("        SELECT <include refid=\"Base_Column_List\" /> FROM \n");
		sb.append("        <include refid=\"Table_Name\" />\n");
		sb.append("        WHERE id = #{id}\n");
		sb.append("    </select>\n\n");

		// queryAll
		sb.append("    <!-- 查询所有 -->\n");
		sb.append("    <select id=\"queryAll\" parameterType=\"java.util.Map\" resultType=\"" + beanName + "\">\n");
		sb.append("        SELECT <include refid=\"Base_Column_List\" /> FROM \n");
		sb.append("        <include refid=\"Table_Name\" />\n");
		sb.append("        <include refid=\"condition\" />\n");
		sb.append("        <include refid=\"splitPages\" />\n");
		sb.append("    </select>\n\n");

		// queryCount
		sb.append("    <!-- 查询总数 -->\n");
		sb.append("    <select id=\"queryCount\" parameterType=\"java.util.Map\" resultType=\"java.lang.Long\">\n");
		sb.append("        SELECT COUNT(*) FROM \n");
		sb.append("        <include refid=\"Table_Name\" />\n");
		sb.append("        <include refid=\"condition\" />\n");
		sb.append("    </select>\n\n");

		// uptsql
		sb.append("    <sql id=\"updateSQL\">\n");
		String uptsql = "";
		for (int i = 0; i < fileds.size(); i++) {
			String f = fileds.get(i);
			if ("id".equals(f)) {
				continue;
			}
			if (isNotEmpty(f)) {
				uptsql += "        <if test=\"" + f + " != null\">" + f + " = #{" + f + "},</if>\n";
			}
		}
		sb.append(uptsql + "\n");
		sb.append("    </sql>\n\n");

		// con
		sb.append("    <sql id=\"condition\">\n");
		sb.append("        <where>\n");
		String consql = "";
		for (int i = 0; i < fileds.size(); i++) {
			String f = fileds.get(i);
			if ("id".equals(f)) {
				continue;
			}
			if (isNotEmpty(f)) {
				consql += "            <if test=\"" + f + " != null\">AND " + f + " = #{" + f + "} </if>\n";
			}
		}
		sb.append(consql + "\n");
		sb.append("            <if test=\"skey != null\">AND id LIKE concat('%',#{skey},'%')</if>\n");

		sb.append("      </where>\n");
		sb.append("    </sql>\n\n");

		// split
		sb.append("    <sql id=\"splitPages\">\n");
		sb.append("        <if test=\"sortFiled != null\">\n");
		sb.append("            ORDER BY ${sortFiled}\n");
		sb.append("        </if>\n");
		sb.append("        <if test=\"sortFiled != null and direction != null \">\n");
		sb.append("            ${direction}\n");
		sb.append("        </if>\n");
		sb.append("        <if test=\"start!=null\">\n");
		sb.append("            LIMIT #{start}\n");
		sb.append("        </if>\n");
		sb.append("        <if test=\"start!=null and perpage!=null\">\n");
		sb.append("            ,#{perpage}\n");
		sb.append("        </if>\n");
		sb.append("    </sql>\n\n");
		sb.append("</mapper>\n");
		//
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + beanName + "Dao.xml");
	}

	// 4-1,根据读取的信息，创建实体service,{entity}Service.java\{entity}ServiceImpl.java
	public void createService(String codePath) {
		// 创建包
		String javaFilePath = codePath + "service";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		sb.append("package " + packageName + "." + moduleName + ".service;\n\n");
		sb.append("import java.util.*;\n");
		sb.append("import " + packageName + "." + moduleName + ".bean." + beanName + ";\n\n");

		sb.append("public interface " + beanName + "Service{\n\n");

		sb.append("    int addBean(" + beanName + " bean);\n\n");
		sb.append("    int updateBean(" + beanName + " bean);\n\n");
		sb.append("    int deleteBean(String id);\n\n");
		sb.append("    " + beanName + " queryBean(String id);\n\n");
		sb.append("    List<" + beanName + "> queryAll(Map<String, Object> map);\n\n");
		sb.append("    long queryCount(Map<String, Object> map);\n\n");

		sb.append("}\n");
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + beanName + "Service.java");
	}

	// 4-2,根据读取的信息，创建实体service,{entity}Service.java\{entity}ServiceImpl.java
	public void createServiceImpl(String codePath) {
		// 创建包
		String javaFilePath = codePath + "service" + File.separator + "impl";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		sb.append("package " + packageName + "." + moduleName + ".service.impl;\n\n");
		sb.append("import java.util.*;\n\n");

		sb.append("import org.springframework.beans.factory.annotation.Autowired;\n");
		sb.append("import org.springframework.stereotype.Service;\n\n");

		sb.append("import " + packageName + "." + moduleName + ".bean." + beanName + ";\n");
		sb.append("import " + packageName + "." + moduleName + ".dao." + beanName + "Dao;\n");
		sb.append("import " + packageName + "." + moduleName + ".service." + beanName + "Service;\n\n");

		sb.append("@Service\n");
		sb.append("public class " + beanName + "ServiceImpl  implements " + beanName + "Service {\n\n");

		String dao = beanName.toLowerCase() + "Dao";
		sb.append("    @Autowired\n");
		sb.append("    private " + beanName + "Dao " + dao + ";\n\n");

		sb.append("    public int addBean(" + beanName + " bean){\n");
		sb.append("        return " + dao + ".addBean(bean);\n");
		sb.append("    }\n\n");

		sb.append("    public int updateBean(" + beanName + " bean){\n");
		sb.append("        return " + dao + ".updateBean(bean);\n");
		sb.append("    }\n\n");

		sb.append("    public int deleteBean(String id){\n");
		sb.append("        return " + dao + ".deleteBean(id);\n");
		sb.append("    }\n\n");

		sb.append("    public " + beanName + " queryBean(String id){\n");
		sb.append("        return " + dao + ".queryBean(id);\n");
		sb.append("    }\n\n");

		sb.append("    public List<" + beanName + "> queryAll(Map<String, Object> map){\n");
		sb.append("        return " + dao + ".queryAll(map);\n");
		sb.append("    }\n\n");

		sb.append("    public long queryCount(Map<String, Object> map){\n");
		sb.append("        return " + dao + ".queryCount(map);\n");
		sb.append("    }\n\n");

		sb.append("}\n");
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + beanName + "ServiceImpl.java");
	}

	// 5,根据读取的信息，创建实体controller
	public void createWebController(String codePath) {
		// 创建包
		String javaFilePath = codePath + "web" + File.separator + "controller";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		sb.append("package " + packageName + "." + moduleName + ".web.controller;\n\n");

		sb.append("import java.util.*;\n\n");

		sb.append("import org.springframework.context.annotation.Scope;\n");
		sb.append("import org.springframework.web.bind.annotation.*;\n");
		sb.append("import org.springframework.stereotype.Controller;\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\n");
		sb.append("import org.springframework.ui.Model;\n\n");

		sb.append("import com.aat.dxfy.utils.Atools;\n");
		sb.append("import com.aat.dizena.base.web.controller.BaseController;\n\n");

		sb.append("import " + packageName + "." + moduleName + ".bean." + beanName + ";\n");
		sb.append("import " + packageName + "." + moduleName + ".service." + beanName + "Service;\n\n");

		sb.append("@Controller\n@Scope(\"prototype\")\n@RequestMapping(\"master/" + beanName.toLowerCase() + "\")\n");
		sb.append("public class Master" + beanName + "Controller  extends BaseController{\n\n");
		// base str
		sb.append("    @Autowired\n");
		sb.append("    private " + beanName + "Service " + beanName.toLowerCase() + "Service;\n\n");

		sb.append("    private String list = \"/master/" + beanName.toLowerCase() + "/list\";\n");
		sb.append("    private String edit = \"/master/" + beanName.toLowerCase() + "/edit\";\n");
		sb.append("    private String reList = \"redirect:\" + list;\n");
		sb.append("    private String addAction = \"master/" + beanName.toLowerCase() + "/add\";\n");
		sb.append("    private String uptAction = \"master/" + beanName.toLowerCase() + "/upt\";\n");
		sb.append("    private String delAction = \"master/" + beanName.toLowerCase() + "/del\";\n");

		sb.append("    private String StringKey = \" \";\n");
		sb.append("    private String addTitle = \"增加\"+StringKey;\n");
		sb.append("    private String uptTitle = \"修改\"+StringKey;\n");
		sb.append("    private String listHead = StringKey+\"管理\";\n\n");

		// list
		sb.append("    // list get\n");
		sb.append("    @RequestMapping(value = \"list\", method = RequestMethod.GET)\n");
		sb.append("    public String list(Model m) {\n");
		sb.append("        m.addAttribute(\"addAction\", addAction);\n");
		sb.append("        m.addAttribute(\"uptAction\", uptAction);\n");
		sb.append("        m.addAttribute(\"delAction\", delAction);\n");
		sb.append("        m.addAttribute(\"listHead\", listHead);\n");
		sb.append("        m.addAttribute(\"list\", list);\n");
		sb.append("        return list;\n");
		sb.append("    }\n\n");

		sb.append("    // list post\n");
		sb.append("    @RequestMapping(value = \"list\", method = RequestMethod.POST)\n");
		sb.append("    @ResponseBody\n");

		sb.append(
				"    public Map<String, Object> list(@RequestParam(value = \"s\", required = false, defaultValue = \"1\") int start,\n");
		sb.append("            @RequestParam(value = \"p\", required = false, defaultValue = \"10\") int perPage,\n");
		sb.append(
				"            @RequestParam(value = \"f\", required = false, defaultValue = \"ts\") String sortFiled,\n");
		sb.append(
				"            @RequestParam(value = \"dir\", required = false, defaultValue = \"DESC\") String direction,\n\n");
		sb.append("        @RequestParam(value = \"u\", required = false) String skey) {\n");

		sb.append("        Map<String, Object> params = new HashMap<String, Object>();\n");
		sb.append("        params.put(\"sortFiled\", sortFiled);\n");
		sb.append("        params.put(\"direction\", direction);\n");
		sb.append("        params.put(\"start\", (start - 1) * perPage);\n");
		sb.append("        params.put(\"perpage\", perPage);\n");
		sb.append("        // 其他参数\n");
		sb.append("        if (isNotEmpty(skey)) {\n");
		sb.append("            params.put(\"skey\", skey);\n");
		sb.append("        }\n\n");

		sb.append("        //  返回参数\n");
		sb.append("        List<" + beanName + "> lists = " + beanName.toLowerCase() + "Service.queryAll(params);\n");
		sb.append("        Long count = " + beanName.toLowerCase() + "Service.queryCount(params);\n");
		sb.append("        Long tmp = count / perPage;\n");
		sb.append("        Long pages = count % perPage == 0 ? tmp : tmp + 1;\n");
		sb.append("        Map<String, Object> json = new HashMap<String, Object>();\n");

		sb.append("        json.put(\"datas\", lists);// 数据\n");
		sb.append("        json.put(\"pages\", pages);// 总页\n");
		sb.append("        json.put(\"start\", start);// 页码\n");
		sb.append("        json.put(\"count\", count);// 总数\n");
		sb.append("        return json;\n");
		sb.append("    }\n\n");
		// list end

		// add
		sb.append("    // add get\n");
		sb.append("    @RequestMapping(value = \"add\", method = RequestMethod.GET)\n");
		sb.append("    public String add(Model model) {\n");
		sb.append("        model.addAttribute(\"action\", addAction);\n");
		sb.append("        model.addAttribute(\"title\", addTitle);\n");

		sb.append("        " + beanName + " m = new " + beanName + "();\n");
		sb.append("        m.setId(Atools.getOneKeyS());\n");
		sb.append("        model.addAttribute(\"m\", m);\n");
		sb.append("        return edit;\n");
		sb.append("    }\n\n");

		sb.append("    // add post\n");
		sb.append("    @RequestMapping(value = \"add\", method = RequestMethod.POST)\n");
		sb.append("    public String add(" + beanName + " m,Model model) {\n");
		sb.append("        model.addAttribute(\"action\", addAction);\n");
		sb.append("        model.addAttribute(\"title\", addTitle);\n");
		sb.append("        model.addAttribute(\"m\", m);\n");

		sb.append("        // 验证\n\n");
		sb.append("        //end \n");
		sb.append("        " + beanName.toLowerCase() + "Service.addBean(m);\n");
		sb.append("        return reList;\n");
		sb.append("    }\n\n");
		// add end

		// upt
		sb.append("    // upt get\n");
		sb.append("    @RequestMapping(value = \"upt/{id}\", method = RequestMethod.GET)\n");
		sb.append("    public String upt(@PathVariable(\"id\") String id,Model model) {\n");
		sb.append("        model.addAttribute(\"action\", uptAction);\n");
		sb.append("        model.addAttribute(\"title\", uptTitle);\n");

		sb.append("        " + beanName + " m = " + beanName.toLowerCase() + "Service.queryBean(id);\n");
		sb.append("        model.addAttribute(\"m\", m);\n");
		sb.append("        return edit;\n");
		sb.append("    }\n\n");

		sb.append("    // upt post\n");
		sb.append("    @RequestMapping(value = \"upt\", method = RequestMethod.POST)\n");
		sb.append("    public String upt(" + beanName + " m,Model model) {\n");
		sb.append("        model.addAttribute(\"action\", uptAction);\n");
		sb.append("        model.addAttribute(\"title\", uptTitle);\n");
		sb.append("        model.addAttribute(\"m\", m);\n");

		sb.append("        // 验证\n\n");
		sb.append("        //end \n");
		sb.append("        " + beanName.toLowerCase() + "Service.updateBean(m);\n");
		sb.append("        return reList;\n");
		sb.append("    }\n\n");
		// upt end

		// del
		sb.append("    // del\n");
		sb.append("    @RequestMapping(value = \"del/{id}\", method = RequestMethod.GET)\n");
		sb.append("    public String del(@PathVariable(\"id\") String id) {\n");
		sb.append("        " + beanName.toLowerCase() + "Service.deleteBean(id);\n");
		sb.append("        return reList;\n");
		sb.append("    }\n\n");
		// del end

		// ok
		sb.append("}\n");
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + "Master" + beanName + "Controller.java");
	}

	// 102：创建文件包
	private void createPackage(String pathname) {
		File f = new File(pathname);
		if (!f.exists()) {
			f.mkdirs();
		}
	}

	// 103：判断不为空
	private boolean isNotEmpty(String str) {
		if (null == str) {
			return false;
		}
		if ("".equals(str) || str.length() == 0) {
			return false;
		}
		return true;

	}

	// 104：写出文件
	private void writeS(String str, String encoding, String sfile) {
		char[] cuf = str.toCharArray();// 将指定的字串序列化为byte[]
		try {
			int lastIndex = sfile.lastIndexOf(File.separator);
			String sfileDir = sfile.substring(0, lastIndex);
			File file = new File(sfileDir);
			if (!file.exists()) {
				file.mkdirs();
			}
			OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(sfile), encoding);
			out.write(cuf);
			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	// 得到con
	public Connection getCon() {
		try {
			if (con == null) {
				Class.forName(driver);
				con = DriverManager.getConnection(url, user, pwd);
			}
		} catch (Exception e) {
		}
		return con;

	}

	// 得到表
	public List<String> getTabNames() {
		List<String> tabs = new ArrayList<String>();
		try {
			Statement s = getCon().createStatement();
			ResultSet rs = s.executeQuery("show tables");
			while (rs.next()) {
				tabs.add(rs.getString(1));
			}
		} catch (Exception e) {
		}
		return tabs;
	}

	// 101：根据字段类型，得到java的属性类型；
	public String getFiledType(String str) {
		String st = str.toLowerCase();
		if (st.contains("char")) {
			return "String";
		}
		if (!st.contains("bigint") && st.contains("int")) {
			return "Integer";
		}
		if (st.contains("bigint")) {
			return "Long";
		}
		if (st.contains("float") || st.contains("double") || st.contains("num") || st.contains("dec")) {
			return "Double";
		}
		if (st.contains("date") || st.contains("time")) {
			return "Date";
		}
		if (st.contains("bit") || st.contains("real")) {
			return "Boolean";
		}
		return "String";
	}

	// 得到表字段-类型
	public void getTabFileds(String tabName) {
		try {
			fileds.clear();
			types.clear();
			String sql = "desc " + tabName;
			Statement s = getCon().createStatement();
			ResultSet rs = s.executeQuery(sql);
			while (rs.next()) {
				String key = rs.getString(1);
				String type = rs.getString(2);
				String val = "";
				if (type.contains("bigint")) {
					val = "Long";
				} else if ((!type.contains("bigint")) && type.contains("int")) {
					val = "Integer";
				} else if (type.contains("float") || type.contains("double") || type.contains("num")
						|| type.contains("dec")) {
					val = "Double";
				} else if (type.contains("char")) {
					val = "String";
				} else if (type.contains("date") || type.contains("time")) {
					val = "java.util.Date";
				} else if (type.contains("bit") || type.contains("real")) {
					val = "Boolean";
				}else{
					val = "String";
				}
				fileds.add(key);
				types.add(val);
			}

		} catch (Exception e) {
		}
	}

	public void close() {
		if (con != null) {
			try {
				con.close();
			} catch (SQLException e) {
			}
		}
		System.out.println("--------over--------");
	}
	//

}
